package cn.iocoder.yudao.module.project.controller.admin.deliveryreceiptdetail;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.project.controller.admin.deliveryreceiptdetail.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.deliveryreceiptdetail.DeliveryReceiptDetailDO;
import cn.iocoder.yudao.module.project.service.deliveryreceiptdetail.DeliveryReceiptDetailService;

@Tag(name = "管理后台 - 拨交单明细")
@RestController
@RequestMapping("/project/delivery-receipt-detail")
@Validated
public class DeliveryReceiptDetailController {

    @Resource
    private DeliveryReceiptDetailService deliveryReceiptDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建拨交单明细")
    @PreAuthorize("@ss.hasPermission('project:delivery-receipt-detail:create')")
    public CommonResult<Integer> createDeliveryReceiptDetail(@Valid @RequestBody DeliveryReceiptDetailSaveReqVO createReqVO) {
        return success(deliveryReceiptDetailService.createDeliveryReceiptDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新拨交单明细")
    @PreAuthorize("@ss.hasPermission('project:delivery-receipt-detail:update')")
    public CommonResult<Boolean> updateDeliveryReceiptDetail(@Valid @RequestBody DeliveryReceiptDetailSaveReqVO updateReqVO) {
        deliveryReceiptDetailService.updateDeliveryReceiptDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除拨交单明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('project:delivery-receipt-detail:delete')")
    public CommonResult<Boolean> deleteDeliveryReceiptDetail(@RequestParam("id") Integer id) {
        deliveryReceiptDetailService.deleteDeliveryReceiptDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得拨交单明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('project:delivery-receipt-detail:query')")
    public CommonResult<DeliveryReceiptDetailRespVO> getDeliveryReceiptDetail(@RequestParam("id") Integer id) {
        DeliveryReceiptDetailDO deliveryReceiptDetail = deliveryReceiptDetailService.getDeliveryReceiptDetail(id);
        return success(BeanUtils.toBean(deliveryReceiptDetail, DeliveryReceiptDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得拨交单明细分页")
    //@PreAuthorize("@ss.hasPermission('project:delivery-receipt-detail:query')")
    public CommonResult<PageResult<DeliveryReceiptDetailRespVO>> getDeliveryReceiptDetailPage(@Valid DeliveryReceiptDetailPageReqVO pageReqVO) {
        PageResult<DeliveryReceiptDetailDO> pageResult = deliveryReceiptDetailService.getDeliveryReceiptDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeliveryReceiptDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出拨交单明细 Excel")
    @PreAuthorize("@ss.hasPermission('project:delivery-receipt-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeliveryReceiptDetailExcel(@Valid DeliveryReceiptDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeliveryReceiptDetailDO> list = deliveryReceiptDetailService.getDeliveryReceiptDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "拨交单明细.xls", "数据", DeliveryReceiptDetailRespVO.class,
                        BeanUtils.toBean(list, DeliveryReceiptDetailRespVO.class));
    }

    @GetMapping("/getReport")
    @Operation(summary = "获得拨交单明细报表")
    public CommonResult<PageResult<DeliveryReceiptDetailRespVO>> getReport(@Valid DeliveryReceiptDetailPageReqVO pageReqVO) {
        PageResult<DeliveryReceiptDetailDO> pageResult = deliveryReceiptDetailService.getReport(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeliveryReceiptDetailRespVO.class));
    }

    @GetMapping("/getDeliveryReceiptSummaryReport")
    @Operation(summary = "获得拨交单统计报表")
    public CommonResult<PageResult<DeliveryReceiptSummaryRespVO>> getDeliveryReceiptSummaryReport(@Valid DeliveryReceiptDetailPageReqVO pageReqVO) {
        PageResult<DeliveryReceiptSummaryRespVO> pageResult = deliveryReceiptDetailService.getDeliveryReceiptSummaryReport(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/exportDeliverReport")
    @Operation(summary = "导出拨交单明细 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeliverReport(@Valid DeliveryReceiptDetailPageReqVO pageReqVO,
                                                 HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeliveryReceiptDetailDO> list = deliveryReceiptDetailService.getReport(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "拨交单明细-明细.xls", "数据", DeliveryReceiptDetailReportVO.class,
                BeanUtils.toBean(list, DeliveryReceiptDetailReportVO.class));
    }

    @GetMapping("/exportDeliverSummaryReport")
    @Operation(summary = "导出拨交单汇总 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeliverSummaryReport(@Valid DeliveryReceiptDetailPageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeliveryReceiptSummaryRespVO> list = deliveryReceiptDetailService.getDeliveryReceiptSummaryReport(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "拨交单明细-汇总.xls", "数据", DeliveryReceiptSummaryRespVO.class,
                list);
    }


}